package com.zhenyi.tuangou.admin.index;

import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.zhenyi.common.model.TgCategory;
import com.zhenyi.common.model.TgGroupProduct;
import com.zhenyi.common.model.TgProduct;
import com.zhenyi.common.model.TgProductItem;

public class ProductController extends AdminController {

	@Override
	public void list() {
		Kv kv = Kv.by("shopId", getShopId());
		Long typeId = getParaToLong("typeId");
		if (typeId != null) {
			kv.set("typeId", typeId);
		}
		String search = getPara("search");
		if (StrKit.notBlank(search)) {
			kv.set("search", search);
		}
		SqlPara sqlPara = Db.getSqlPara("FindAdminProducts", kv);
		int page = getPage();
		Page<Record> pages = Db.paginate(page, PAGE_SIZE, sqlPara);
		renderPage(pages);
	}

	/**
	 * 团购列表
	 */
	public void groupProducts() {
		Long productId = getParaToLong("productId");
		SqlPara sqlPara = Db.getSqlPara("FindAdminProductGroupItems", productId);
		Page<Record> pages = Db.paginate(getPage(), PAGE_SIZE, sqlPara);
		renderPage(pages);

	}

	/**
	 * 产品子类
	 */
	public void items() {
		Long productId = getParaToLong("productId");
		SqlPara sqlPara = Db.getSqlPara("FindAdminProductItems", productId);
		Page<Record> pages = Db.paginate(getPage(), PAGE_SIZE, sqlPara);
		renderPage(pages);
	}

	/**
	 * 商品详情
	 */
	@Override
	public void info() {
		Long productId = getParaToLong("productId");
		if (productId == null) {
			renderFailure(500, "id is null");
			return;
		}
		TgProduct product = TgProduct.dao.findById(productId);
		if (product == null) {
			renderFailure(404, "product not found");
			return;
		}
		if (product.getIsGroup() == 1) {
			SqlPara sqlPara = Db.getSqlPara("FindAdminProductGroupItems", product.getId());
			List<Record> list = Db.find(sqlPara);
			product.put("size", list);
		} else {
			SqlPara sqlPara = Db.getSqlPara("FindAdminProductItems", product.getId());
			List<Record> list = Db.find(sqlPara);
			product.put("size", list);
		}
		renderSuccess(product);
	}

	/**
	 * 更新类目
	 */
	public void updateCategory() {
		Long productId = getParaToLong("productId");
		Long categoryId = getParaToLong("categoryId");
		TgCategory category = TgCategory.dao.findById(categoryId);
		if (category == null) {
			renderFailure();
			return;
		}
		TgProduct product = TgProduct.dao.findById(productId);
		product.setCategoryId(categoryId);
		product.update();
		renderSuccess();
	}

	@Override
	public void submit() {
		Long id = getParaToLong("id");
		String name = getPara("name");
		String images = getPara("images");
		String desc = getPara("desc");
		Integer price = getParaToInt("price");
		String cover = getPara("cover");
		Long type = getParaToLong("type");
		String html = getPara("html");
		Integer isGroup = getParaToInt("isGroup");
		Integer stock = getParaToInt("stock");
		
		boolean isNew = id == null;
		TgProduct info = isNew ? new TgProduct() : TgProduct.dao.findById(id);
		if (info == null) {
			renderFailure(404, "product not found");
			return;
		}
		info.setName(name);
		info.setImages(images);
		info.setDesc(desc);
		info.setPrice(price);
		info.setShopId(getShopId());
		info.setCover(cover);
		info.setType(type);
		info.setHtml(html);
		info.setIsGroup(isGroup);
		info.setStock(stock);
		if (isNew) {
			info.setAddTime();
		}
		
		boolean flag = isNew ? info.save() : info.update();
		if (flag) {
			renderSuccess();
		} else {
			renderFailure();
		}
	}
	
	public void submitGroupProduct() {
		Long id = getParaToLong("id");
		Integer num = getParaToInt("num");
		Integer price = getParaToInt("price");
		Long productId = getParaToLong("productId");
		boolean isNew = id == null;
		TgGroupProduct info = isNew ? new TgGroupProduct() : TgGroupProduct.dao.findById(id);
		if (!isNew && info == null) {
			renderFailure(404, "group product not found");
			return;
		}
		info.setNum(num);
		info.setPrice(price);
		info.setProductId(productId);
		boolean flag = isNew ? info.save() : info.update();
		if (flag) {
			renderSuccess();
		} else {
			renderFailure();
		}
	}
	
	public void delGroupProduct() {
		Long id = getParaToLong("id");
		if (id == null) {
			renderFailure(500, "id is null");
			return;
		}
		TgGroupProduct info = TgGroupProduct.dao.findById(id);
		if (info == null) {
			renderFailure(404, "group product not found");
			return;
		}
		boolean flag = info.delete();
		if (flag) {
			renderSuccess();
		} else {
			renderFailure();
		}
	}
	
	public void submitProductItem() {
		Long id = getParaToLong("id");
		String itemName = getPara("itemName");
		Integer price = getParaToInt("price");
		Long productId = getParaToLong("productId");
		boolean isNew = id == null;
		TgProductItem info = isNew ? new TgProductItem() : TgProductItem.dao.findById(id);
		if (!isNew && info == null) {
			renderFailure(404, "group product not found");
			return;
		}
		info.setItemName(itemName);
		info.setPrice(price);
		info.setProductId(productId);
		if (isNew) {
			info.setAddTime();
		}
		boolean flag = isNew ? info.save() : info.update();
		if (flag) {
			renderSuccess();
		} else {
			renderFailure();
		}
	}
	
	public void delProductItem() {
		Long id = getParaToLong("id");
		if (id == null) {
			renderFailure(500, "id is null");
			return;
		}
		TgProductItem info = TgProductItem.dao.findById(id);
		if (info == null) {
			renderFailure(404, "product item not found");
			return;
		}
		boolean flag = info.delete();
		if (flag) {
			renderSuccess();
		} else {
			renderFailure();
		}
	}

}
